<?php
include 'PublicdetailController.php';
class TreeController extends PublicdetailController {
	private $_MTree;
	private $_Tree = array ();
	private $idnguoidung;
    private $nguoidung;
	
	public function init() {
		$this->initValue ();
		$this->_MTree = new Model_Tree();
		$this->idnguoidung = $this->TblUserbyId[0]['id'];
        $this->nguoidung = $this->TblUserbyId[0]['name'];		
		if (isset ( $_REQUEST ['name'] ) && $_REQUEST ['name'] != '') {
			$varisCat = (isset ( $_POST ["isCats"] ) && $_POST ["isCats"] != "") ? $_POST ["isCats"] : "";
			if ($varisCat == 'on'){
				$varisCat = 1;}
			else{
				$varisCat = 0;
			    }
			$this->_Tree = array (
					'name' => $_REQUEST ['name'], 
					'type' => $_REQUEST ['type'],
					'isCat' => $varisCat,
					'parentId' => $_REQUEST ['parentId'],
					'price' => $_REQUEST ['price'],
					'quantity' => $_REQUEST ['quantity'],
					'iduser' => $this->idnguoidung ,
					'nameuser' => $this->nguoidung,
					'status' => 1,
					'content' => $_REQUEST ['content'],
				    'createdDate' =>  date('Y-m-d H:i:s'), 
			);
		}
	}
	public function indexAction() {
	 $this->view->roles = $this->TblUserbyId[0]['roles'];
	}
	public function addAction() {
if($this->TblUserbyId[0]['roles']!='Admin' && $this->TblUserbyId[0]['roles']!='Quanly')
		{
		echo "<script type=\"text/javascript\">
			    alert('Bạn không có quyền vào mục này!');
				 window.history.back();
			</script>";
		die;
		}
		else
		$this->_helper->layout ()->disableLayout ();
		$jsonObj = array ();
		
		$dup = $this->_MTree->dupliObj ( 0, @$_REQUEST ['name']);
		if ($dup > 0) {
			echo "<script type=\"text/javascript\">
			    alert('Tên bạn nhập đã có trong dữ liệu!');
			</script>";
			$jsonObj ["success"] = false;
			return $this->view->items  = json_encode ( $jsonObj );
		} else {
			$this->_MTree->add ( $this->_Tree );
			$jsonObj ["success"] = true;
		}
		return $this->view->items  = json_encode ( $jsonObj );
	}
	
	public function updateAction() {
	if($this->TblUserbyId[0]['roles']!='Admin' && $this->TblUserbyId[0]['roles']!='Quanly')
		{
		echo "<script type=\"text/javascript\">
			    alert('Bạn không có quyền vào mục này!');
				 window.history.back();
			</script>";
		die;
		}
		else
		$id = $this->_getParam ( 'id' );
		$this->_helper->layout ()->disableLayout ();
		if($this->_Tree){
			$this->_MTree->update ( $id, $this->_Tree );
			$jsonObj ["success"] = true;
		}
		return $this->view->items = json_encode ( $jsonObj );
	}
	/**
	 * - XÃ³a dá»¯ liá»‡u.
	 * 
	 * @return void string
	 */
	public function delAction() {
		$this->_helper->layout ()->disableLayout ();
		$id = $_REQUEST ['id'];
		// $id = $this->_getParam ( 'id' );
		$_Id = null;
		$jsonObjv = null;
		
		if (isset ( $id ) && $id > 0) {
			$_Id = $this->_MTree->del ( $id );
		} else {
			return;
		}
		
		if (! $_Id) {
			$jsonObj = json_encode ( array (
					'success' => false 
			) );
		} else {
			$jsonObj = json_encode ( array (
					'success' => true 
			) );
		}
		return $this->view->items = $jsonObj;
	}
	
	
	public function childrenssAction() {
		$this->_helper->layout ()->disableLayout ();
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
        $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;  
        $offset = ($page-1)*$rows;    
        $id = isset($_POST['id']) ? intval($_POST['id']) :1;  
		$jsonObj = json_encode($this->_MTree->getchidelpage($id,$rows,$offset));
		return $this->view->items = $jsonObj;
		
	}
	public function childrenAction() {
		$this->_helper->layout ()->disableLayout ();
		$model = new Model_Tree();
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) :1;
		$jsonObj = json_encode($this->_MTree->getItems($id));
		return $this->view->items = $jsonObj;
		
	}
    public function childrensAction() {
		$this->_helper->layout ()->disableLayout ();
		$model = new Model_Tree();
		$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) :1;
		$jsonObj = json_encode($this->_MTree->getItemscombo($id));
		return $this->view->items = $jsonObj;
		
	}
	public function preDispatch() {
		$auth = Zend_Auth::getInstance ();
		if (! $auth->hasIdentity ()) {
			if ($this->_request->getActionName () != 'login') {
				$this->_redirect ( '/index/login' );
			}
		}
	}
	
	public function searAction() {
		$this->_helper->layout ()->disableLayout ();
		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$sort = isset ( $_POST ['sort'] ) ? strval ( $_POST ['sort'] ) : 'Name';
		$order = isset ( $_POST ['order'] ) ? strval ( $_POST ['order'] ) : 'asc';
		$offset = ($page - 1) * $rows;
		$ten = $this->_getParam ( 'Name' );
		$kieu = $this->_getParam ( 'types' );
		try {
			$jsonObj = json_encode ( $this->_MTree->searObj ( $ten,$kieu, $sort, $order, $offset, $rows ) );
			return $this->view->jsonObj = $jsonObj;
		} catch ( Exception $e ) {
			echo "<script type=\"text/javascript\">
			alert('Exception!');
			</script>";
		}
	}
	
	public function isstatusAction() {      
		$id =  @$_REQUEST['id'];
		$ketqua = array (
					'status' => 1,				
			);
		$this->_helper->layout ()->disableLayout ();
		if($id){
			$this->_MTree->isstatus( $id, $ketqua );
		}
		die;
		return $this->view->items = json_encode ( $jsonObj );
		
	}
	
	public function bccthangAction() {   
        $this->_helper->layout ()->disableLayout ();	
		$id =  @$_REQUEST['id'];
		$jsons = json_encode($this->_MTree->getbcsp( $id));
		return $this->view->items = $jsons ;
		
	}
	
}